﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteBuilder
{
    class LineManager
    {
        static List<Line> Lines = new List<Line>();
        static Texture2D Texture1x1;

        public static void init(GraphicsDevice graphicsdevice)
        {
            Texture1x1 = new Texture2D(graphicsdevice, 1, 1, false, SurfaceFormat.Color);
            Texture1x1.SetData(new[] { Color.White });
        }

        public static Line addLine(Vector2 Start,Vector2 End, float lineWidth, Color lineColor, float Layer)
        {
            Line NewLine = new Line(Start, End, lineWidth, lineColor,Layer);
            Lines.Add(NewLine);
            return NewLine;
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < Lines.Count; i++)
            {
                float angle = (float)Math.Atan2(Lines[i].End.Y - Lines[i].Start.Y, Lines[i].End.X - Lines[i].Start.X);
                float length = (Lines[i].End - Lines[i].Start).Length();

                spriteBatch.Draw(Texture1x1, Lines[i].Start, null, Lines[i].lineColor,
                           angle, Vector2.Zero, new Vector2(length, Lines[i].lineWidth),
                           SpriteEffects.None, Lines[i].Layer);
            }
        }
    }

    class Line
    {
        Vector2 _Start;
        public Vector2 Start { get { return _Start; } set { _Start = value; } }
        Vector2 _End;
        public Vector2 End { get { return _End; } set { _End = value; } }
        float _lineWidth;
        public float lineWidth { get { return _lineWidth; } set { _lineWidth = value; } }
        Color _lineColor;
        public Color lineColor { get { return _lineColor; } set { _lineColor = value; } }
        float _Layer;
        public float Layer { get { return _Layer; } set { _Layer = value; } }

        public Line(Vector2 start, Vector2 end, float linewidth, Color linecolor, float layer)
        {
            _Start = start;
            _End = end;
            _lineWidth = linewidth;
            _lineColor = linecolor;
            _Layer = layer;
        }

    }
}
